#include <REGX52.H>
#include "Timer0.h"
#include "KeyPro.h"
#include "NixiePro.h"
#include "Delay.h"
#include "I2C.h"
#include "AT24C02.h"

unsigned char KeyNum,RunFlag;
unsigned char Min,Sec,MiniSec;

void Sec_Loop()
{
	if(RunFlag==0)
	{
		MiniSec++;
		if(MiniSec>=100)
		{
			MiniSec = 0;
			Sec++;
			if(Sec>=60)
			{
				Sec = 0;
				Min++;
				if(Min>=60)
				{
					Min = 0;
				}
			}	
		}		
	}
}

void main()
{
	Timer0_Init();
	while(1)
	{
		KeyNum=Key();
		if(KeyNum==1)//暂停计时（非暂定显示）
		{
			RunFlag =! RunFlag;
		}
		if(KeyNum==2)//归零
		{
			Min = 0;
			Sec = 0;
			MiniSec = 0;
		}	
		if(KeyNum==3)//写入
		{
			AT24C02_WriteByte(0,MiniSec);
			AT24C02_WriteByte(1,Sec);
			AT24C02_WriteByte(2,Min);			
		}
		if(KeyNum==4)//读出
		{
			MiniSec = AT24C02_ReadByte(0);
			Sec = AT24C02_ReadByte(1);
			Min = AT24C02_ReadByte(2);			
		}
		Nixie(1,Min/10);
		Nixie(2,Min%10);
		Nixie(3,17);
		Nixie(4,Sec/10);
		Nixie(5,Sec%10);
		Nixie(6,17);
		Nixie(7,MiniSec/10);
		Nixie(8,MiniSec%10);					
	}
}

void Timer0_Routine() interrupt 1
{
	static unsigned int T0Count1,T0Count2,T0Count3;
	TL0 = 0x18;				//设置定时初始值
	TH0 = 0xFC;				//设置定时初始值
	T0Count1++;//用于按键
	if(T0Count1>=20)
	{
		T0Count1 = 0;
		Key_Loop();	
	}
	T0Count2++;//用于数码管
	if(T0Count2>=2)
	{
		T0Count2 = 0;
		Nixie_Loop();	
	}
	T0Count3++;//用于秒表
	if(T0Count3>=10)
	{
		T0Count3 = 0;	
		Sec_Loop();
	}
	
}